package cn.tannn.surenessdemo.controller;

import cn.jdevelops.annotation.mapping.PathRestController;
import cn.jdevelops.result.result.ResultVO;
import cn.tannn.surenessdemo.constant.RoleConstant;
import cn.tannn.surenessdemo.constant.UserConstant;
import cn.tannn.surenessdemo.controller.dto.LoginDTO;
import cn.tannn.surenessdemo.controller.dto.RegisterDTO;
import cn.tannn.surenessdemo.user.service.AccountService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.usthe.sureness.provider.annotation.WithoutAuth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;

/**
 * 用户登录
 *
 * @author tnnn
 * @version V1.0
 * @date 2022-07-18 13:42
 */
@PathRestController
@Slf4j
@Api(tags = "登录管理", value = "登录管理")
@RequiredArgsConstructor
@ApiSupport(order = 3)
public class LoginController {

    /**
     * 用户
     */
    private final AccountService accountService;


    /**
     * 登录
     * ps: WithoutAuth放行接口
     *
     * @param login login
     * @return ResultVO
     */
    @ApiOperation(value = "登录", notes = "登录管理")
    @PostMapping("/login")
    @WithoutAuth(mapping = "/login/*", method = "post")
    @ApiOperationSupport(order = 1)
    public ResultVO<Object> login(@RequestBody @Valid LoginDTO login) {
        String jwt = accountService.authenticateAccount(login);
        Map<String, String> responseData = Collections.singletonMap("token", jwt);
        if (log.isDebugEnabled()) {
            log.debug("issue token success, account: {} -- token: {}", login, jwt);
        }
        return ResultVO.successForData(responseData);
    }

    @ApiOperation(value = "注册", notes = "登录管理")
    @PostMapping("/register")
    @WithoutAuth(mapping = "/register/*", method = "post")
    @ApiOperationSupport(order = 2)
    public ResultVO<String> accountRegister(@RequestBody @Validated RegisterDTO register, HttpServletRequest request) {
        if (UserConstant.ADMINISTRATORS.equalsIgnoreCase(register.getUsername())) {
            return ResultVO.fail("非法注册用户");
        }
        if (accountService.registerAccount(register, request)) {
            if (log.isDebugEnabled()) {
                log.debug("account: {}, sign up success", register);
            }
            return ResultVO.success("注册成功");
        } else {
            return ResultVO.fail("用户已经存在,请修改用户名后在注册");
        }
    }


    @ApiOperation(value = "退出", notes = "登录管理")
    @PostMapping("/logout")
    @ApiOperationSupport(order = 3)
    public ResultVO<String> logout(HttpServletRequest request) {
        accountService.logout(request);
        return ResultVO.success("成功退出");

    }
}
